import JDBC.JDBCUtil;

import java.sql.*;
import java.util.Scanner;

public class Unilt extends JDBCUtil {
    public static void main(String[] args) {
        String password=null;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = scanner.nextLine();
        do {
            System.out.println("请输入密码：");
            password = scanner.nextLine();
        }while (password.length()!=6);

       /* boolean flag = login(name, password);
        System.out.println(flag ? "登入成功......" : "登入失败~~~~~");*/
        boolean result=register(name,password);
        System.out.println(result ? "注册成功......" : "注册失败~~~~~");
    }
//用户登入
    public static boolean login(String name, String password) {
        boolean flag = false;
        /*Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcl", "kz", "root");
            statement = connection.createStatement();
            String sql = "select * from master where username='" + name + "' and userpassword='" + password + "'";
            resultSet = statement.executeQuery(sql);*/
        try {
             connection = JDBCUtil.getConnection();
            String sql = "select * from master where username=? and userpassword=?";
             statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setString(2,password);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return flag;
    }
//用户注册
    public static boolean register(String name, String password){
       /* Connection connection = null;
        Statement statement = null;*/

        boolean flag = false;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcl", "kz", "root");

            String sql = "insert into master values ('"+1005+"',?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setString(2,password);
            int i = statement.executeUpdate();
            System.out.println("i  = "+i);
            if (i==1) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {

                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return flag;
    }
}
